package com.xydtech.ecard.config.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.config.ServerConfig;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.config.domain.EcardParamConfig;
import com.xydtech.ecard.config.service.IEcardParamConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 电子名片参数配置Controller
 *
 * @author JHON
 * @date 2021-08-06
 */
@Controller
@RequestMapping("/ecard/config/param")
public class EcardParamConfigController extends BaseController {
    private String prefix = "ecard/config/param";

    @Autowired
    private IEcardParamConfigService ecardParamConfigService;
    @Autowired
    private ServerConfig serverConfig;

    @RequiresPermissions("ecard:config:param:view")
    @GetMapping()
    public String param() {
        return prefix + "/param";
    }

    /**
     * 查询电子名片参数配置列表
     */
    @RequiresPermissions("ecard:config:param:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardParamConfig ecardParamConfig) {
        startPage();
        List<EcardParamConfig> list = ecardParamConfigService.selectEcardParamConfigList(ecardParamConfig);
        return getDataTable(list);
    }

    /**
     * 导出电子名片参数配置列表
     */
    @RequiresPermissions("ecard:config:param:export")
    @Log(title = "电子名片参数配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardParamConfig ecardParamConfig) {
        List<EcardParamConfig> list = ecardParamConfigService.selectEcardParamConfigList(ecardParamConfig);
        ExcelUtil<EcardParamConfig> util = new ExcelUtil<EcardParamConfig>(EcardParamConfig.class);
        return util.exportExcel(list, "param");
    }

    /**
     * 新增电子名片参数配置
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存电子名片参数配置
     */
    @RequiresPermissions("ecard:config:param:add")
    @Log(title = "电子名片参数配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardParamConfig ecardParamConfig, HttpServletRequest request) {
        //富文本summerNote内容图片路径保存，ContextPath替换处理（ContextPath配置后缀使用）
        ecardParamConfig.setOrgDesc(serverConfig.replaceContextPath(request, ecardParamConfig.getOrgDesc()));
        /**查询法人机构是否配置*/
        EcardParamConfig paramConfig = new EcardParamConfig();
        paramConfig.setDeptId(ecardParamConfig.getDeptId());
        List<EcardParamConfig> list = ecardParamConfigService.selectEcardParamConfigList(paramConfig);
        if (list.size() > 0) {
            return error("机构已配置，不可以重复配置！");
        }
        return toAjax(ecardParamConfigService.insertEcardParamConfig(ecardParamConfig));
    }

    /**
     * 修改电子名片参数配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, HttpServletRequest request, ModelMap mmap) {
        EcardParamConfig ecardParamConfig = ecardParamConfigService.selectEcardParamConfigById(id);
        //富文本summerNote内容图片显示，ContextPath替换处理（ContextPath配置后缀使用）
        String contextPath = request.getServletContext().getContextPath();
        if (contextPath.length() > 1 && ecardParamConfig.getOrgDesc() != null) {
            ecardParamConfig.setOrgDesc(ecardParamConfig.getOrgDesc().replaceAll("/profile", contextPath + "/profile"));
        }
        mmap.put("ecardParamConfig", ecardParamConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子名片参数配置
     */
    @RequiresPermissions("ecard:config:param:edit")
    @Log(title = "电子名片参数配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardParamConfig ecardParamConfig, HttpServletRequest request) {
        //富文本summerNote内容图片路径保存，ContextPath替换处理（ContextPath配置后缀使用）
        ecardParamConfig.setOrgDesc(serverConfig.replaceContextPath(request, ecardParamConfig.getOrgDesc()));
        return toAjax(ecardParamConfigService.updateEcardParamConfig(ecardParamConfig));
    }

    /**
     * 删除电子名片参数配置
     */
    @RequiresPermissions("ecard:config:param:remove")
    @Log(title = "电子名片参数配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ecardParamConfigService.deleteEcardParamConfigByIds(ids));
    }
}
